package com.itheima.d3_charset;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * 字符集
 * 字符的编码和解码
 */
public class CharSetDemo1 {
    public static void main(String[] args) throws UnsupportedEncodingException {
        //1.编码:字符串 -> 字节数组
        String name = "眉如远山a";
        byte[] bytes = name.getBytes();//默认使用UTF-8 一个汉字占三个字节
        System.out.println(bytes.length);
        System.out.println(Arrays.toString(bytes));

        //2.解码:字节数组 -> 字符串
        String name2 = new String(bytes);//默认使用UTF-8 解码
        System.out.println(name2);


        //可以指定编码格式和解码格式
        String name3 = "正梅花万里雪深时1";
        byte[] bytes2 = name3.getBytes("GBK");//GBK编码
        System.out.println(bytes2.length);
        System.out.println(Arrays.toString(bytes2));

        String name4 = new String(bytes2,"GBK");//GBK解码
        System.out.println(name4);

    }
}
